# -*- coding: utf-8 -*-

from model import Collection, db
from time import time
import hashlib

EXPIRED_TIME = 3600  #验证码失效时间

class Pin(Collection):
    '''
    用户找回密码所需的验证码
* `_id` (ObjectId) - ID
* `email` (string) - email of user
* `code` (string) - identifying code
* `createTime` (long) - 验证码生成时间(时间戳)
    '''
    collection = db.pin

    class Field(object):
        _id = '_id'
        email = 'email'
        code = 'code'
        createTime = 'createTime'

    @staticmethod
    def create_pin(email, code):
        return Pin.insert({ 
            Pin.Field.email: email,
            Pin.Field.code: code,
            Pin.Field.createTime: int(time())
        })

    @staticmethod
    def delete_expired_pin():
        return Pin.remove({ 
            Pin.Field.createTime: {'$lt': (int(time()) - EXPIRED_TIME)}  # 小于1小时
        })
        
